﻿-- Author  : Kurapica
-- Create Date : 2009/10/12

--[[
	In-Game Addon System
	Studio Part
	DesignPanel
--]]
do
	IGAS:NewAddon("IGAS_Studio")

	-- DesignPanel
	local designPanel = CreateFrame("TabGroup", "DesignPanel", coreF)
	local marker = CreateFrame(Widget.Marker, "Marker", coreF)
	local selector = CreateFrame(Widget.Selector, "Selector", coreF)
	local widgetPanel = CreateFrame(Widget.WidgetList, "WidgetPanel", coreF)
	local propertyPanel = CreateFrame(Widget.PropertyList, "PropertyPanel", coreF)
	marker.InMarkMode = true
	selector.InSelectedMode = false

	------------------------------------------------
	-- Scripts
	------------------------------------------------

	-- designPanel's script
	function designPanel:OnTabChange()
		propertyPanel.SelectedFrame = nil
		marker.MarkedFrame = nil
	end

	function designPanel:OnTabClose()
		marker.MarkedFrame = nil
		propertyPanel.SelectedFrame = nil
		designPanel:GetSelectTab().Container.Designer:Clear()
	end

	-- Selector's script
	function selector:OnAreaSelected(stF, stX, stY, endX, endY)
		-- Check if stF is a ShadowFrame, use it's parent instead of itself
		if stF:IsObjectType(Widget.ShadowFrame) then
			stF = stF.Parent
		end

		Log(1, "Add Frame "..widgetPanel.SelectedWidget.WidgetName)

		local frame = designPanel:GetSelectTab().Container.Designer:AddFrame(widgetPanel.SelectedWidget, nil, stF)

		-- Use marker not frame self to set points
		if frame:IsObjectType("Frame") then
			frame.FrameLevel = stF.FrameLevel + 1
		end
		if frame:IsObjectType("Region") then
			frame:ClearAllPoints()
			frame.Width = endX - stX
			frame.Height = stY - endY
			frame:SetPoint("TOPLEFT", stF, "TOPLEFT", stX - stF:GetLeft(), stY - stF:GetTop())
		end

		widgetPanel:DisSelected()
	end

	-- propertyPanel's script
	function propertyPanel:OnPropertyChanged(frame, prop, value, oldvalue)
		-- Action for property : Name
		if prop == "Name" then
			propertyPanel.SelectedFrame = nil
			_FrameInfoList[frame].LastName = frame.Name
			designPanel:GetSelectTab().Container.Designer:RefreshList()
			propertyPanel.SelectedFrame = frame
		end
	end

	function propertyPanel:OnFrameAdvance(frame)
		if not frame then
			return
		end
		frame = GetTrueFrame(frame)
		if frame and Advance[GetTrueObject(frame)] and Advance[GetTrueObject(frame)]["SetFrame"] then
			Advance[GetTrueObject(frame)]:SetFrame(frame)
			Advance[GetTrueObject(frame)]:Show()
		end
	end

	function propertyPanel:OnFrameDispose(frame)
		if not frame then
			return
		end
		marker.MarkedFrame = nil
		propertyPanel.SelectedFrame = nil
		designPanel:GetSelectTab().Container.Designer:RemoveFrame(frame)
	end

	------------------------------------------------
	-- Custom Events
	------------------------------------------------
	designPanel:RegisterEvent("IGAS_STUDIO_FILEOPEN_REQUIRE")
	designPanel:RegisterEvent("IGAS_STUDIO_DESIGNEROPEN_REQUIRE")
	designPanel:RegisterEvent("IGAS_STUDIO_WIDGET_SELECTED")
	designPanel:RegisterEvent("IGAS_STUDIO_SAVE_FILE")
	designPanel:RegisterEvent("IGAS_STUDIO_FRAME_SELECTED")
	designPanel:RegisterEvent("IGAS_STUDIO_FRAME_ADVANCED")
	designPanel:RegisterEvent("IGAS_STUDIO_FILERELOAD_REQUIRE")
	designPanel:RegisterEvent("IGAS_STUDIO_FILECLOSE_REQUIRE")

	function designPanel:IGAS_STUDIO_FILEOPEN_REQUIRE(addon, file, part)
		local name

		if file.Type == "Frame" then
			name = file.Name.."."..part
		else
			name = file.Name
		end

		if not designPanel:GetTab(name) then
			local tab = designPanel:AddTab(name)
			tab:FixHeight()
			CreateFrame(Widget.Script, "Designer", tab.Container, addon, file, part):Load()
		end
	end

	function designPanel:IGAS_STUDIO_DESIGNEROPEN_REQUIRE(addon, file)
		if not designPanel:GetTab(file.Name..".Designer") then
			local tab = designPanel:AddTab(file.Name..".Designer")
			tab:FixHeight()
			CreateFrame(Widget.Designer, "Designer", tab.Container, addon, file):Load()
		end
	end

	function designPanel:IGAS_STUDIO_WIDGET_SELECTED(widget)
		if widget then
			marker.InMarkMode = false
			selector.InSelectedMode = true
		else
			marker.InMarkMode = true
			selector.InSelectedMode = false
		end
	end

	function designPanel:IGAS_STUDIO_SAVE_FILE(saveall)
		if saveall then
			for i = 1, designPanel.TabNum do
				designPanel:GetTabByIndex(i).Container.Designer:Save()
			end
		else
			if designPanel:GetSelectTab() then
				designPanel:GetSelectTab().Container.Designer:Save()
			end
		end
	end

	function designPanel:IGAS_STUDIO_FRAME_SELECTED(frame)
		propertyPanel.SelectedFrame = frame
		designPanel:GetSelectTab().Container.lstVirtual:SelectItemByValue(frame)
	end

	function designPanel:IGAS_STUDIO_FRAME_ADVANCED(frame)
		designPanel:GetSelectTab().Container.Designer:Advance(frame)
	end

	function designPanel:IGAS_STUDIO_FILERELOAD_REQUIRE(file)
		for i = 1, designPanel.TabNum do
			if designPanel:GetTabByIndex(i).Container.Designer.__File == file then
				designPanel:GetTabByIndex(i).Container.Designer:Load()
			end
		end
	end

	function designPanel:IGAS_STUDIO_FILECLOSE_REQUIRE(file)
		for i = 1, designPanel.TabNum do
			if designPanel:GetTabByIndex(i).Container.Designer.__File == file then
				designPanel:GetTabByIndex(i):Dispose()
			end
		end
	end
end
